var pool = require("./config/db.js"),
    markdown = require('markdown').markdown;

/**
 * 类型
 * @param ratings
 * @constructor
 */
function Roomtypes(roomtypes){
    if(roomtypes){
        this.hotel_id=roomtypes.hotel_id;
        this.rooms_number=roomtypes.rooms_number;
        this.room_type=roomtypes.room_type;
        this.description=roomtypes.description;
        this.minibar=roomtypes.minibar;
        this.bathtub=roomtypes.bathtub;
        this.safe=roomtypes.safe;
        this.tv=roomtypes.tv;
        this.balcony=roomtypes.balcony;
        this.radio=roomtypes.radio;
        this.phone=roomtypes.phone;
        this.ac=roomtypes.ac;
        this.bathacc=roomtypes.bathacc;
        this.wc=roomtypes.wc;
        this.dryer=roomtypes.dryer;
        this.desk=roomtypes.desk;
        this.towels=roomtypes.towels;
        this.view=roomtypes.view;
        this.heating=roomtypes.heating;
        this.picture1=roomtypes.picture1;
        this.picture2=roomtypes.picture2;
        this.picture2=roomtypes.picture2;
    }
};

module.exports = Roomtypes;

/**
 * 增
 * @param callback
 */
Roomtypes.prototype.save = function(callback) {
    var _obj = {
        hotel_id:this.hotel_id,
        rooms_number:this.rooms_number,
        room_type:this.room_type,
        description:this.description,
        minibar:this.minibar,
        bathtub:this.bathtub,
        safe:this.safe,
        tv:this.tv,
        balcony:this.balcony,
        radio:this.radio,
        phone:this.phone,
        ac:this.ac,
        bathacc:this.bathacc,
        wc:this.wc,
        dryer:this.dryer,
        desk:this.desk,
        towels:this.towels,
        view:this.view,
        heating:this.heating,
        picture1:this.picture1,
        picture2:this.picture2
    };
    pool.getConnection(function(err,connection){
        connection.query('INSERT INTO `roomtypes` SET ?',
            _obj,
            function (err, rows, fields) {
                if (err) throw err;
                connection.release();
                callback(null,rows);
            });
    });
};

/**
 * 删一个
 * @param callback
 */
Roomtypes.prototype.delete = function(id,callback) {
    var _id= id;
    pool.getConnection(function(err,connection){
        connection.query('DELETE FROM `roomtypes` WHERE hotel_id=?',
            [_id],
            function (err, rows, fields) {
                if (err) throw err;
                connection.release();
                callback(null,rows);
            });
    });
};

/**
 * 改一个
 * @param callback
 */
Roomtypes.prototype.update = function(callback) {
    var _obj = {
        hotel_id:this.hotel_id,
        rooms_number:this.rooms_number,
        room_type:this.room_type,
        description:this.description,
        minibar:this.minibar,
        bathtub:this.bathtub,
        safe:this.safe,
        tv:this.tv,
        balcony:this.balcony,
        radio:this.radio,
        phone:this.phone,
        ac:this.ac,
        bathacc:this.bathacc,
        wc:this.wc,
        dryer:this.dryer,
        desk:this.desk,
        towels:this.towels,
        view:this.view,
        heating:this.heating,
        picture1:this.picture1,
        picture2:this.picture2
    };
    pool.getConnection(function(err,connection){
        connection.query('UPDATE `roomtypes` SET ? WHERE hotel_id =?',
            [
                _obj,
                _obj.hotel_id
            ],
            function (err, rows, fields) {
                if (err) throw err;
                connection.release();
                callback(null,rows);
            });
    });
};

/**
 * 查一个
 * @param callback
 */
Roomtypes.prototype.getOne = function(id,callback) {
    var _id= id;
    pool.getConnection(function(err,connection){
        connection.query('SELECT * from `roomtypes` WHERE hotel_id=?',
            [_id]
            , function (err, rows, fields) {
                if (err) throw err;
                connection.release();
                callback(null,rows);
            });
    });
};

/**
 * 分页查
 * @param pagesize 每页个数
 * @param pages    当前页
 * @param callback
 */
Roomtypes.getTen = function(pagesize,pages,callback) {
    var _size = pagesize ? pagesize  : 10,
        _page = pages ? pages : 1,
        _total = null,
        _totalPage=null;

    pool.getConnection(function(err,connection){
        if(err){
            callback("连接池出错");
        }
        if(_total == null){
            connection.query('SELECT COUNT(*) FROM `roomtypes`',function(err,rows,fields){
                _total = rows[0]['COUNT(*)'];
                _totalPage = _total / _size;
                _query();
            });
        }
        var _query = function(){
            connection.query('SELECT * from `roomtypes` ORDER BY `hotel_id` DESC LIMIT ?,?',
                [_totalPage>=1 ? (_page-1)*_size : 0,
                    _totalPage <_page ? (_page*_size) :_size]
                , function (err, rows, fields) {
                    if (err){
                        callback("查询出错");
                    }
                    connection.release();
                    callback(null,rows,_totalPage);
                });
        }
    });
};
